package com.noble.meether.fragment.user;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.noble.glamour.bean.center.user.detail.UserDetail;
import com.noble.glamour.module.logic.application.App;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.library.image.ImageLoader;
import com.noble.library.view.base.BasePanel;
import com.noble.meether.MeetUIShow;
import com.noble.meether.R;

/**
 * 个人中心条目头部
 */
public class UserFragmentHeadPanel extends BasePanel implements View.OnClickListener {

    private ImageView user_head, user_gender, user_vip;
    private TextView user_nick, user_id;

    private UserDetail myInfo;

    public UserFragmentHeadPanel(Context context) {
        super(context);
        setContentView(R.layout.p1_user_fragment_header);

        initView();
        refreshView();
    }

    private void initView() {
        user_head = (ImageView) findViewById(R.id.user_head);
        user_vip = (ImageView) findViewById(R.id.user_vip);
        user_gender = (ImageView) findViewById(R.id.user_gender);
        user_nick = (TextView) findViewById(R.id.user_nick);
        user_id = (TextView) findViewById(R.id.user_id);
        findViewById(R.id.iv_set).setOnClickListener(this);
        ImageLoader.loadCircleAvatar(getContext(), R.drawable.default_head, user_head);

        //根据屏幕分辨率设置最大显示长度
        user_nick.setMaxEms(App.context.getResources().getDisplayMetrics().density <= 1.5 ? 5 : 7);

        user_head.setOnClickListener(this);
        user_nick.setOnClickListener(this);
        findViewById(R.id.rl_info).setOnClickListener(this);
    }

    /**
     * 界面刷新
     */
    public void refreshView() {
        refreshHeader();
        user_id.setText("ID号：" + myInfo.getUid());
        user_nick.setText(myInfo.getNickname());
        if (myInfo.isMan())
            user_gender.setBackgroundResource(R.drawable.icon_man);
    }

    private void refreshHeader() {
        myInfo = ModuleMgr.getCenterMgr().getMyInfo();
        refreshVip();
        ImageLoader.loadCircleAvatar(getContext(), myInfo.getAvatar(), user_head);
    }

    private void refreshVip() {
        user_vip.setVisibility(myInfo.isVip() ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.user_head://上传头像
            case R.id.rl_info:
                MeetUIShow.showUserEditInfoAct(getContext());
                break;
            case R.id.iv_set:
                MeetUIShow.showUserSetAct((Activity) getContext(), 100);
                break;
        }
    }
}